﻿

Namespace YahooManaged.Finance.Screener.StockCriterias

    ''' <summary>
    ''' Criteria base class
    ''' </summary>
    ''' <remarks></remarks>
    Public MustInherit Class CriteriaDefinition
        Public MustOverride ReadOnly Property DisplayName As String
        Public MustOverride ReadOnly Property CriteriaName As String
        Friend MustOverride Function CriteriaParameter() As String
        Friend MustOverride ReadOnly Property IsValid As Boolean
    End Class

    ''' <summary>
    ''' Criteria base class for Stock Screener
    ''' </summary>
    ''' <remarks></remarks>
    Public MustInherit Class StockCriteriaDefinition
        Inherits CriteriaDefinition

        ''' <summary>
        ''' The Stock Screener criteria group
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public MustOverride ReadOnly Property CriteriaGroup As StockScreenerCriteriaGroup
        ''' <summary>
        ''' The quote properties that the specific criteria class represents/provides
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public MustOverride ReadOnly Property ProvidedQuoteProperties As QuoteProperty()
        ''' <summary>
        ''' The Stock Screener properties that the specific criteria class represents/provides
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public MustOverride ReadOnly Property ProvidedScreenerProperties As StockScreenerProperty()
        Friend Property CriteriaTag As String

        Friend Overrides ReadOnly Property IsValid As Boolean
            Get
                Return Me.CriteriaTag <> String.Empty
            End Get
        End Property

        Protected Sub New(ByVal paramType As String)
            Me.CriteriaTag = paramType
        End Sub
    End Class



    Public Interface IPercentageAvailability
        ''' <summary>
        ''' Indicates if the compared values are in percent or absolute
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Property PercentValues As Boolean
    End Interface

    Public Interface IGainLoss
        Property GainOrLoss As StockPriceChangeDirection
    End Interface

    Public Interface IExtremeParameter
        Property ExtremeParameter As StockExtremeParameter
    End Interface

    Public Interface ILessGreater
        Property LessGreater As LessGreater
    End Interface

    Public Interface IUpDown
        Property UpDown As UpDown
    End Interface

    Public Interface IMovingAverage
        Property MovingAverage As MovingAverageType
    End Interface

    Public Interface IRelativeTimeSpan
        Property RelativeTimeSpanInMinutes As StockTradingTimeSpan
    End Interface

    Public Interface IRelativeTimePoint
        Property TimeSpanRelativeTo As StockTradingRelativeTimePoint
    End Interface

    Public Interface IAbsoluteTimePoint
        Property ValueRelativeTo As StockTradingAbsoluteTimePoint
    End Interface

    ''' <summary>
    ''' Criteria base class for Stock Screener and digit values
    ''' </summary>
    ''' <remarks></remarks>
    Public MustInherit Class StockDigitCriteriaDefinition
        Inherits StockCriteriaDefinition

        Private mConverterCulture As New Globalization.CultureInfo("en-US")

        ''' <summary>
        ''' The maximum value of the value range.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property MinimumValue As Nullable(Of Double) = Nothing
        ''' <summary>
        ''' The minimum value of the value range
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property MaximumValue As Nullable(Of Double) = Nothing

        Friend Property OptionalParamValue As Integer = -1
        Friend Overrides ReadOnly Property IsValid As Boolean
            Get
                Return MyBase.IsValid AndAlso (Me.MaximumValue.HasValue Or Me.MinimumValue.HasValue)
            End Get
        End Property

        Protected Sub New(ByVal paramType As String)
            MyBase.New(paramType)
            Me.MinimumValue = 0
        End Sub

        Friend Overrides Function CriteriaParameter() As String
            If Me.IsValid Then
                Return "&" & MyBase.CriteriaTag & "=" & IIf(Me.OptionalParamValue <> -1, Me.OptionalParamValue.ToString & "_", "").ToString & Me.GetParamDigitValue(Me.MinimumValue) & "_" & Me.GetParamDigitValue(Me.MaximumValue) & "_e_3"
            Else
                Throw New NotSupportedException("The parameters are invalid.")
            End If
        End Function

        Private Function GetParamDigitValue(ByVal paramValue As Nullable(Of Double)) As String
            If paramValue.HasValue Then
                Return paramValue.Value.ToString(mConverterCulture)
            Else
                Return "u"
            End If
        End Function

    End Class



    ''' <summary>
    ''' Criteria base class for Stock Screener and string values
    ''' </summary>
    ''' <remarks></remarks>
    Public MustInherit Class StockStringCriteriaDefinition
        Inherits StockCriteriaDefinition

        Friend Property Value As String = String.Empty
        Friend Overrides ReadOnly Property IsValid As Boolean
            Get
                Return MyBase.IsValid AndAlso Me.Value <> String.Empty
            End Get
        End Property

        Protected Sub New(ByVal paramType As String)
            MyBase.New(paramType)
        End Sub

        Friend Overrides Function CriteriaParameter() As String
            If Me.IsValid Then
                Return "&" & MyBase.CriteriaTag & "=" & Me.Value & "_e_3"
            Else
                Throw New NotSupportedException("The parameters are invalid.")
            End If
        End Function

    End Class

End Namespace